package org.dev.vlp.controller;

import org.dev.vlp.bean.JsonResponse;
import org.dev.vlp.constants.Roles;
import org.dev.vlp.entity.Community;
import org.dev.vlp.entity.Tag;
import org.dev.vlp.interceptor.RequiredPermission;
import org.dev.vlp.service.ComUserService;
import org.dev.vlp.service.CommunityService;
import org.dev.vlp.service.MomentService;
import org.dev.vlp.service.TagService;
import org.dev.vlp.utils.PageResult;
import org.dev.vlp.vo.CommunityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Description
 * Param
 * return
 * Author:86184
 * Date: 2023/1/8
 */
@RestController
@RequestMapping("/community")
@RequiredPermission({Roles.ROLE_USER})
public class CommunityController extends BaseController{

    @Autowired
    private CommunityService communityService;
    @Autowired
    private ComUserService comUserService;

    @Autowired
    private MomentService momentService;

    @PutMapping
    public JsonResponse<Object> insert(@RequestBody @Valid Community community) {
        communityService.insert(community,(int)getUid());
        return JsonResponse.success();
    }

    @PostMapping
    public JsonResponse<Object> update(@RequestBody @Valid Community community) {
        communityService.modify(community,(int)getUid());
        return JsonResponse.success();
    }

    @GetMapping
    public JsonResponse<List<Community>> selectCom(@Valid String name) {
        List<Community> communities=communityService.selectByName(name);
        return JsonResponse.success(communities);
    }

    @DeleteMapping("/{comId}")
    public JsonResponse<Object> deleteCom(@PathVariable("comId") int comId) {
        communityService.delete(comId,(int)getUid());
        return JsonResponse.success();
    }

    @GetMapping("/{comId}")
    public JsonResponse<CommunityVo> details(@PathVariable("comId") int comId) {
        CommunityVo details = communityService.details(comId,(int)getUid());
        return JsonResponse.success(details);
    }

    @PutMapping("/{comId}")
    public JsonResponse<Object> join(@PathVariable("comId") int comId) {
        comUserService.joinCommunity(comId,(int) getUid());
        return JsonResponse.success();
    }

    @GetMapping("/me")
    public JsonResponse<List<Community>> getMe() {

        return JsonResponse.success(communityService.getMyCom((int) getUid()));
    }
    @GetMapping("/joined")
    public JsonResponse<List<Community>> getJoined() {

        return JsonResponse.success(comUserService.getMyJoined((int) getUid()));
    }

    @GetMapping("/moment/{comId}")
    public JsonResponse<PageResult> queryTopicMoment(@PathVariable("comId") int topicId,
                                                     @RequestParam int page,
                                                     @RequestParam int size) {
        PageResult momentVos = momentService.queryComMoment(topicId, size, page);
        return JsonResponse.success(momentVos);
    }
}
